﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebMaster_Shop : System.Web.UI.Page
{
    private readonly string m_FolderImage = GlobalClassBE.FolderPhotoData + @"Shop\";

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPage();
    }

    #region load page

    private void LoadPage()
    {
        string func = "";
        if (Request.Params[GlobalClassBE.MyUrlParameter.ActionName] != null)
            func = Request.Params[GlobalClassBE.MyUrlParameter.ActionName].ToString();

        pnlList.Visible = false;
        pnlAddEdit.Visible = false;
        pnlDelete.Visible = false;

        if (func == "" || func == "list")
        {
            pnlList.Visible = true;
            LoadList();
        }
        else if (func == "add" || func == "edit")
        {
            pnlAddEdit.Visible = true;
            LoadInput(func);
        }
        else if (func == "delete")
        {
            pnlDelete.Visible = true;
            LoadDelete();
        }

        GlobalClassBE.MyMessage.MgsCurrentTitle = GlobalClassBE.PageName.ShopTitle;
    }

    private void LoadList()
    {
        int pageSize = GlobalClassBE.PageSize;
        DataTable dtData = GetDataSource();
        int k = 0;
        if (dtData != null)
            k = dtData.Rows.Count;
        this.gssPaging.PageCount = k / pageSize;
        if (this.gssPaging.PageCount * pageSize != k)
            this.gssPaging.PageCount += 1;
        this.grdData.PageSize = pageSize;
        this.gssPaging.PageSize = pageSize;
        this.gssPaging.ItemCount = k;
        if (this.gssPaging.CurrentIndex <= 0 || this.gssPaging.CurrentIndex > this.gssPaging.PageCount)
            this.gssPaging.CurrentIndex = 1;
        string[] astr = new string[1];
        GssDO.DOShop DO = new GssDO.DOShop();
        astr[0] = DO.PKIDShopColumnName;
        GlobalClassBE.SetGridCss(this.grdData);
        this.ShowTextForList();
        this.grdData.AutoGenerateColumns = false;
        this.grdData.AllowPaging = true;
        this.grdData.DataKeyNames = astr;
        this.grdData.DataSource = dtData;
        this.grdData.DataBind();

        // remove cahe
        HttpContext.Current.Cache.Remove("idCity");
        HttpContext.Current.Cache.Remove("idDistrict");

        if (dtData == null || dtData.Rows.Count == 0)
            pnlData.Visible = false;
    }

    private void LoadInput(string actName)
    {
        ShowTextForInput();
        ckbDeleteImages.Attributes.Add("onClick", "checkUpload(this,'" + fulImages.ClientID + "')");
        if (!Page.IsPostBack)
        {
            string sID = "";
            if (Request.Params["id"] != null)
                sID = Request.Params["id"].ToString();

            GlobalClassBE.MyMessage.MgsCurrentMessages = "";

            if (actName == "add")
                ShowToAdd();
            else if (actName == "edit")
            {
                if (sID != "")
                    ShowToEdit(sID);
                else
                {
                    GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                    CancelPage();
                }
            }
            else
            {
                CancelPage();
            }
        }
    }

    private void LoadDelete()
    {
        string id = "";
        if (Request.Params["id"] != null)
            id = Request.Params["id"].ToString();

        GlobalClassBE.MyMessage.MgsCurrentMessages = "";

        if (id != "")
            ShowToDelete(id);
        else
            CancelPage();
    }

    #endregion load page

    #region show text

    private void ShowTextForList()
    {
        GssDO.DOShop DO = new GssDO.DOShop();
        Hashtable htb = new Hashtable();
        htb.Add(DO.TitleColumnName, GlobalClassBE.GridText.Title);
        htb.Add(DO.EmailColumnName, GlobalClassBE.GridText.Email);
        htb.Add(DO.AddressColumnName, GlobalClassBE.GridText.Address);
        htb.Add(DO.LevelColumnName, GlobalClassBE.GridText.Level);
        lkbAddNew.Text = GlobalClassBE.ActionText.Add;
        for (int i = 0; i < this.grdData.Columns.Count; i++)
        {
            if (this.grdData.Columns[i].GetType().Name == "CommandField")
            {
                if (((CommandField)this.grdData.Columns[i]).ShowEditButton)
                {
                    ((CommandField)this.grdData.Columns[i]).EditText = GlobalClassBE.ActionText.Edit;
                }
                if (((CommandField)this.grdData.Columns[i]).ShowDeleteButton)
                {
                    ((CommandField)this.grdData.Columns[i]).DeleteText = GlobalClassBE.ActionText.Delete;
                }
            }
            else if (this.grdData.Columns[i].Visible)
            {
                string s = ((BoundField)this.grdData.Columns[i]).DataField;
                ((BoundField)this.grdData.Columns[i]).HeaderText = htb[s].ToString();
            }
        }
    }

    private void ShowTextForInput()
    {
        lblTitle.Text = GlobalClassBE.LabelText.Title;
        lblEmail.Text = GlobalClassBE.LabelText.Email;
        lblWebsite.Text = GlobalClassBE.LabelText.Website;
        lblPhone.Text = GlobalClassBE.LabelText.Phone;
        lblAddress.Text = GlobalClassBE.LabelText.Address;
        lblLevel.Text = GlobalClassBE.LabelText.Level;
        lblImage.Text = GlobalClassBE.LabelText.Image;
        lblDescription.Text = GlobalClassBE.LabelText.Description;
        lblState.Text = GlobalClassBE.LabelText.Cities;
        lblLocation.Text = GlobalClassBE.LabelText.Districts;

        lblDeleteImages.Text = GlobalClassBE.LabelText.DeleteImage;

        reqFormatEmail.ErrorMessage = GlobalClassBE.MyMessage.InvalidEmailAddress;
        reqImages.Text = GlobalClassBE.MyMessage.MgsFileType;
        reqFormatNumberLevel.ErrorMessage = GlobalClassBE.MyMessage.MgsNumberFormatFrom0To9999;
        reqField.Text = GlobalClassBE.MyMessage.MgsRequiredField;

        btnSave.Text = GlobalClassBE.ButtonText.BtnSave;
        btnContinuous.Text = GlobalClassBE.ButtonText.BtnSaveCon;
        btnClear.Text = GlobalClassBE.ButtonText.BtnReset;
        btnCancel.Text = GlobalClassBE.ButtonText.BtnCancel;
    }

    private void ShowTextForDelete()
    {
        lblDelete.Text = GlobalClassBE.LabelText.DeleteConfirm;

        btnYes.Text = GlobalClassBE.ButtonText.BtnYes;
        btnNo.Text = GlobalClassBE.ButtonText.BtnNo;
    }

    #endregion show text

    #region load data

    private DataTable GetDataSource()
    {
        DataTable dt = null;
        try
        {
            GssBL.BLShop BL = new GssBL.BLShop();
            dt = BL.getAllDataNotDeleted();
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }

    #endregion load data

    #region SaveState
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.RegisterRequiresControlState(this);
    }
    protected override object SaveControlState()
    {
        object[] state = new object[2];
        object baseState = base.SaveControlState();
        state[0] = baseState;
        state[1] = SelectedItemKeys;
        return state;
    }
    protected override void LoadControlState(object savedState)
    {
        if (savedState == null) return;
        if (savedState is object[])
        {
            object[] state = (object[])savedState;
            base.LoadControlState(state[0]);
            SelectedItemKeys = state[1];
        }
    }
    #endregion SaveState

    #region properties

    protected object m_ItemKeys;
    public virtual object SelectedItemKeys
    {
        get { return m_ItemKeys; }
        set { m_ItemKeys = value; }
    }
    #endregion properties

    /*    list    */

    protected void SelectedIndexChanged(int k)
    {
        this.grdData.SelectedIndex = k;
        m_ItemKeys = this.grdData.DataKeys[k].Value;
    }
    protected void gssPaging_Command(object sender, CommandEventArgs e)
    {
        int i = Convert.ToInt32(e.CommandArgument);
        this.gssPaging.CurrentIndex = i;
        this.grdData.PageIndex = i - 1;
        this.grdData.DataBind();
    }
    protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataControlRowType itemType = e.Row.RowType;
        if (itemType == DataControlRowType.DataRow)
        {
            string s = ((BoundField)this.grdData.Columns[0]).DataField.ToString();
            if (DataBinder.Eval(e.Row.DataItem, s).ToString().CompareTo("&nbsp;") != 0)
            {
                // add event to rows item on grid
                try
                {
                    e.Row.Attributes.Add("onmouseover", "changeColorStyle(this);");
                    e.Row.Attributes.Add("onmouseout", "resetColorStyle(this);");
                }
                catch (Exception exc)
                {
                    string mess = exc.Message.ToString();
                }
            }
        }
    }
    protected void grdData_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int k = e.RowIndex;
        SelectedIndexChanged(k);
        string keys = (string)this.SelectedItemKeys;
        string sLink = String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.Shop, "delete") + "&id=" + keys;
        Response.Redirect(sLink, false);
    }
    protected void grdData_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int k = e.NewEditIndex;
        SelectedIndexChanged(k);
        string keys = (string)this.SelectedItemKeys;
        string sLink = String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.Shop, "edit") + "&id=" + keys;
        // add cache
        GssDO.DOShop DO = getByID(keys);
        if (DO != null)
        {
            if (HttpContext.Current.Cache["IDState"] != null)
                HttpContext.Current.Cache.Remove("IDState");
            HttpContext.Current.Cache.Insert("IDState", DO.StateID);

            if (HttpContext.Current.Cache["IDLocation"] != null)
                HttpContext.Current.Cache.Remove("IDLocation");
            HttpContext.Current.Cache.Insert("IDLocation", DO.LocationID);
        }
        Response.Redirect(sLink, false);
    }
    protected void lkbAddNew_Click(object sender, EventArgs e)
    {
        string sLink = String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.Shop, "add");
        Response.Redirect(sLink, false);
    }

    #region ShowAll

    private void ShowToAdd()
    {
        txtTitle.Text = "";
        txtAddress.Text = "";
        txtEmail.Text = "";
        txtWebsite.Text = "";
        txtLevel.Text = "0";
        txtPhone.Text = "";
        txtDescription.Text = "";

        ckbDeleteImages.Visible = false;
        lblDeleteImages.Visible = false;

        lblActionName.Text = GlobalClassBE.ActionText.Add;
        ShowButton(true);
        GlobalClassBE.MyMessage.MgsCurrentMessages = "";
    }
    private void ShowToEdit(string pID)
    {
        try
        {
            GssDO.DOShop DO = getByID(pID);

            if (DO != null)
            {
                txtTitle.Text = DO.Title;
                txtEmail.Text = DO.Email;
                txtWebsite.Text = DO.Website;
                txtLevel.Text = DO.Level.ToString();
                txtDescription.Text = DO.Description;
                txtAddress.Text = DO.Address;
                txtPhone.Text = DO.Phone;
                int i = 0;
                string s = DO.StateID;
                for (i = 0; i < ddlState.Items.Count; i++)
                    if (ddlState.Items[i].Value == s)
                    {
                        ddlState.SelectedIndex = i;
                        cddState.SelectedValue = s;
                        break;
                    }
                s = DO.LocationID;
                for (i = 0; i < ddlLocation.Items.Count; i++)
                    if (ddlLocation.Items[i].Value == s)
                    {
                        ddlLocation.SelectedIndex = i;
                        cddLocation.SelectedValue = s;
                        break;
                    }

                lblTempID.Text = DO.PKIDShop;
                lblCurrentImages.Text = DO.Image;
                if (DO.Image.Trim() != "")
                {
                    ckbDeleteImages.Visible = true;
                    lblDeleteImages.Visible = true;
                }
                else
                {
                    ckbDeleteImages.Visible = false;
                    lblDeleteImages.Visible = false;
                }
                lblActionName.Text = GlobalClassBE.ActionText.EditPage;
                ShowButton(false);
                GlobalClassBE.MyMessage.MgsCurrentMessages = "";
            }
            else
            {
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                CancelPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message;
        }
    }
    private GssDO.DOShop getByID(string id)
    {
        GssDO.DOShop DO = null;
        try
        {
            GssBL.BLShop BL = new GssBL.BLShop();
            DO = BL.getShopByID(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            DO = null;
        }
        return DO;
    }
    /// <summary>
    /// show button on form (Parameter: true: show to add, false: show to edit)
    /// </summary>
    /// <param name="pFlag">type bool: true: show to add; false: show to edit</param>
    private void ShowButton(bool pFlag)
    {
        btnSave.Visible = true;
        btnClear.Visible = true;
        btnCancel.Visible = true;
        btnContinuous.Visible = pFlag;
    }
    private void CancelPage()
    {
        Response.Redirect(GlobalClassBE.PageName.Shop, false);
    }
    private void ResetPage()
    {
        string actName = "";
        if (Request.Params[GlobalClassBE.MyUrlParameter.ActionName] != null)
            actName = Request.Params[GlobalClassBE.MyUrlParameter.ActionName].ToString();
        string sID = "";
        if (Request.Params["id"] != null)
            sID = Request.Params["id"].ToString();

        if (actName != "" && sID != "")
            Response.Redirect(String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.Shop, "edit") + "&id=" + sID, false);
        else
            Response.Redirect(String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.Shop, "add"), false);
    }
    private void SavePage(bool pFlag)
    {
        lblMessage.Text = "";
        if (validateForm())
        {
            string actName = "";
            if (Request.Params[GlobalClassBE.MyUrlParameter.ActionName] != null)
                actName = Request.Params[GlobalClassBE.MyUrlParameter.ActionName].ToString();
            if (actName == "add")
                SaveAdd(pFlag);
            else if (actName == "edit")
            {
                string sID = lblTempID.Text.ToString();
                if (sID != "")
                    SaveEdit(sID);
                else
                {
                    GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                    CancelPage();
                }
            }
            else
            {
                CancelPage();
            }
        }
    }
    private void SaveAdd(bool pFlag)
    {
        try
        {
            GssDO.DOShop DO = fillDO();
            GssBL.BLShop BL = new GssBL.BLShop();
            bool isUpload = true;
            if (fulImages.FileName.Trim() != "")
            {
                try
                {
                    // create root folder
                    System.IO.Directory.CreateDirectory(m_FolderImage);
                    // upload image to server
                    fulImages.PostedFile.SaveAs(m_FolderImage + DO.Image);
                    // get image from server
                    System.Drawing.Image img = System.Drawing.Image.FromFile(m_FolderImage + DO.Image);
                    // calculate image's width and height
                    int w = img.Width, h = img.Height;
                    double aw = 0, bw = 0;
                    if (w > h)
                    {
                        aw = (w > 300) ? 300 : w;
                        bw = ((double)(aw / w)) * h;
                    }
                    else
                    {
                        bw = (h > 300) ? 300 : h;
                        aw = ((double)(bw / h)) * w;
                    }
                    // create thumbnail
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)aw, (int)bw);
                    using (System.Drawing.Graphics grp = System.Drawing.Graphics.FromImage(bmp))
                    {
                        // prevent using image internal thumbnail
                        img.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                        img.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                        // set interpolation mode is high
                        grp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        // draw image
                        grp.DrawImage(img, 0, 0, (int)aw, (int)bw);
                    }

                    string ext = GlobalClassBE.getFileExtendtion(DO.Image);
                    if (ext == ".jpg")
                        bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Jpeg);
                    else if (ext == ".gif")
                        bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Gif);
                    else if (ext == ".png")
                        bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Png);
                    else if (ext == ".bmp")
                        bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Bmp);

                    img.Dispose();
                    img = null;
                    bmp.Dispose();
                    bmp = null;

                    // delete template image
                    System.IO.File.Delete(m_FolderImage + DO.Image);

                    DO.Image = "s" + DO.Image;

                    isUpload = true;
                }
                catch (System.IO.IOException io)
                {
                    lblMessage.Text = io.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                    isUpload = false;
                }
                catch (Exception e)
                {
                    lblMessage.Text = e.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                    isUpload = false;
                }
            }
            if (isUpload)
            {
                BL.Insert(DO);
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsAdd_Success;
                if (!pFlag)
                    CancelPage();
                else
                    ResetPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsAdd_UnSuccess;
        }
    }
    private void SaveEdit(string pID)
    {
        try
        {
            GssDO.DOShop DO = fillDO(pID);
            GssBL.BLShop BL = new GssBL.BLShop();
            bool isUpload = true;
            if (ckbDeleteImages.Checked)
            {
                try
                {
                    System.IO.File.Delete(m_FolderImage + @"\" + lblCurrentImages.Text);
                    DO.Image = "";
                    isUpload = true;
                }
                catch (System.IO.IOException io)
                {
                    lblMessage.Text = io.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsAccess_Deny;
                    isUpload = false;
                }
                catch (Exception e)
                {
                    lblMessage.Text = e.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsAccess_Deny;
                    isUpload = false;
                }
            }
            else
            {
                if (fulImages.FileName.Trim() != "")
                {
                    try
                    {
                        // create root folder
                        System.IO.Directory.CreateDirectory(m_FolderImage);
                        // upload image to server
                        fulImages.PostedFile.SaveAs(m_FolderImage + DO.Image);
                        // get image from server
                        System.Drawing.Image img = System.Drawing.Image.FromFile(m_FolderImage + DO.Image);
                        // calculate image's width and height
                        int w = img.Width, h = img.Height;
                        double aw = 0, bw = 0;
                        if (w > h)
                        {
                            aw = (w > 300) ? 300 : w;
                            bw = ((double)(aw / w)) * h;
                        }
                        else
                        {
                            bw = (h > 300) ? 300 : h;
                            aw = ((double)(bw / h)) * w;
                        }
                        // create thumbnail
                        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)aw, (int)bw);
                        using (System.Drawing.Graphics grp = System.Drawing.Graphics.FromImage(bmp))
                        {
                            // prevent using image internal thumbnail
                            img.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                            img.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                            // set interpolation mode is high
                            grp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                            // draw image
                            grp.DrawImage(img, 0, 0, (int)aw, (int)bw);
                        }

                        string ext = GlobalClassBE.getFileExtendtion(DO.Image);
                        if (ext == ".jpg")
                            bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Jpeg);
                        else if (ext == ".gif")
                            bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Gif);
                        else if (ext == ".png")
                            bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Png);
                        else if (ext == ".bmp")
                            bmp.Save(m_FolderImage + "s" + DO.Image, System.Drawing.Imaging.ImageFormat.Bmp);

                        img.Dispose();
                        img = null;
                        bmp.Dispose();
                        bmp = null;

                        // delete template image
                        System.IO.File.Delete(m_FolderImage + DO.Image);

                        DO.Image = "s" + DO.Image;

                        isUpload = true;
                    }
                    catch (System.IO.IOException io)
                    {
                        lblMessage.Text = io.Message;
                        lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                        isUpload = false;
                    }
                    catch (Exception e)
                    {
                        lblMessage.Text = e.Message;
                        lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                        isUpload = false;
                    }
                }
            }
            if (isUpload)
            {
                BL.Update(DO);
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Success;
                CancelPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsEdit_UnSuccess;
        }
    }

    private bool validateByLabel()
    {
        bool result = true;
        try
        {
            if (ddlState.SelectedValue == "0" || ddlState.SelectedValue == "")
            {
                reqCity.Visible = true;
                result = false;
            }
            else
                reqCity.Visible = false;
            if (ddlLocation.SelectedValue == "0" || ddlLocation.SelectedValue == "")
            {
                reqDistrict.Visible = true;
                result = false;
            }
            else
                reqDistrict.Visible = false;
            if (!GlobalClassBE.ValidateFileExtention(fulImages, GlobalClassBE.ImagesFile))
            {
                reqImages.Visible = true;
                result = false;
            }
            else
                reqImages.Visible = false;
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }
    private bool validateForm()
    {
        bool result = true;
        try
        {
            if (!reqTitle.IsValid)
                result = false;
            if (!reqLevel.IsValid)
                result = false;
            if (!reqFormatNumberLevel.IsValid)
                result = false;
            if (!reqFormatEmail.IsValid)
                result = false;
            if (result)
            {
                result = validateByLabel();
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }

    private GssDO.DOShop fillDO()
    {
        string sID = GlobalClassBE.CreateGUI();
        GssDO.DOShop DO = fillDO(sID);
        return DO;
    }

    private GssDO.DOShop fillDO(string id)
    {
        GssDO.DOShop DO = getByID(id);
        if (DO == null)
        {
            DO = new GssDO.DOShop();
            DO.Title_EN = "";
            DO.Address_EN = "";
            DO.Description_EN = "";
            DO.IsDeleted = 0;
        }
        try
        {
            DO.PKIDShop = id;
            DO.Title = txtTitle.Text.Trim();
            DO.Address = txtAddress.Text.Trim();
            DO.Phone = txtPhone.Text.Trim();
            DO.Email = txtEmail.Text.Trim();
            DO.Website = txtWebsite.Text.Trim();
            DO.Level = Convert.ToInt16(txtLevel.Text);
            DO.Description = txtDescription.Text.Trim();
            DO.StateID = ddlState.SelectedValue;
            DO.LocationID = ddlLocation.SelectedValue;

            if (!ckbDeleteImages.Checked)
            {
                if (fulImages.FileName.Trim() != "")
                    DO.Image = id + GlobalClassBE.getFileExtendtion(fulImages.FileName.Trim());
                else
                    DO.Image = lblCurrentImages.Text;
            }
            else
                DO.Image = "";
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return DO;
    }
    #endregion ShowAll

    /***** Add Edit ******/
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SavePage(false);
    }
    protected void btnContinuous_Click(object sender, EventArgs e)
    {
        SavePage(true);
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ResetPage();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CancelPage();
    }

    #region delete

    private void ShowToDelete(string id)
    {
        lblActionName.Text = GlobalClassBE.ActionText.DeletePage;

        ShowTextForDelete();

        string deleteMessage = GlobalClassBE.MyMessage.DeletedBrachConfirmMessage;

        GssDO.DOShop DO = getByID(id);

        if (DO != null)
        {
            lblDeletedMessage.Text = String.Format(deleteMessage, DO.Title);
        }
        else
            CancelPage();
    }

    private void SaveDelete()
    {
        try
        {
            string id = "";
            if (Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName] != null)
                id = Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName].ToString();

            if (id != "")
            {
                GssDO.DOShop DO = fillDODelete(id);
                GssBL.BLShop BL = new GssBL.BLShop();
                BL.Update(DO);
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsDelete_Success;
                CancelPage();
            }
            else
                CancelPage();
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsDelete_UnSuccess;
        }
    }

    private GssDO.DOShop fillDODelete(string id)
    {
        GssDO.DOShop DO = null;
        try
        {
            DO = getByID(id);
            DO.IsDeleted = 1;
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return DO;
    }
    #endregion delete
    protected void btnYes_Click(object sender, EventArgs e)
    {
        SaveDelete();
    }
    protected void btnNo_Click(object sender, EventArgs e)
    {
        CancelPage();
    }
}
